package top.psjj.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import top.psjj.mall.exception.MallException;
import top.psjj.mall.exception.MallExceptionEnum;
import top.psjj.mall.mapper.CategoryMapper;
import top.psjj.mall.model.po.Category;
import top.psjj.mall.model.vo.CategoryVo;
import top.psjj.mall.service.api.CategoryService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    @CacheEvict(value = "listForCustomer")
    public void add(Category category) {
        Category oldCategory = categoryMapper.selectByName(category.getName());
        if(oldCategory!=null){
            throw new MallException(MallExceptionEnum.EXISTS_NAME);
        }
        categoryMapper.insertSelective(category);
    }

    @Override
    @CacheEvict(value = "listForCustomer")
    public void update(Category category) {
        //1.根据名字查询产品类别
        if(category.getName()!=null){
            Category oldCategory = categoryMapper.selectByName(category.getName());
            if(oldCategory!=null && category.getId()!=oldCategory.getId()){
                throw new MallException(MallExceptionEnum.EXISTS_NAME);
            }
        }
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    @CacheEvict(value = "listForCustomer")
    public void delete(Integer id) {
        categoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo listForAdmin(Integer pageNum, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize,"type,order_num");
        //查询数据
        List<Category> categories = categoryMapper.selectByExample(null);
        //封装分页
        PageInfo pageInfo = new PageInfo(categories);
        return pageInfo;
    }

    @Override
    @Cacheable(value = "listForCustomer")
    public List<CategoryVo> listForCustomer() {
        //查询所有数据
        List<Category> list = categoryMapper.selectByExample(null);
        //将List<Category> 转成List<CategoryVo>
        List<CategoryVo> listVo = new ArrayList<CategoryVo>();
        for(Category category : list){
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category,categoryVo);
            listVo.add(categoryVo);
        }
        //通过集合流获得树型菜单
        List<CategoryVo> tree = getTree(listVo);
        return tree;
    }

    private List<CategoryVo> getTree(List<CategoryVo> listVo) {
        List<CategoryVo> tree = listVo.stream().filter(categoryVo -> {
            if (categoryVo.getParentId() == 0) {
                return true;
            } else {
                return false;
            }
        }).map(categoryVo -> {
            categoryVo.setChildCategory(getChildrenByParentId(categoryVo.getId(),listVo));
            return categoryVo;
        }).sorted((o1,o2)->{
            return o1.getOrderNum()-o2.getOrderNum();
        }).collect(Collectors.toList());
        return tree;
    }
    private List<CategoryVo> getChildrenByParentId(Integer parentId, List<CategoryVo> listVo) {
        List<CategoryVo> result = listVo.stream().filter(categoryVo -> {
            if (categoryVo.getParentId() == parentId) {
                return true;
            } else {
                return false;
            }
        }).map(categoryVo -> {
            categoryVo.setChildCategory(getChildrenByParentId(categoryVo.getId(),listVo));
            return categoryVo;
        }).sorted((o1,o2)->{
            return o1.getOrderNum()-o2.getOrderNum();
        }).collect(Collectors.toList());
        return result;
    }
}
